layui.use(['table', 'admin', 'ax','element','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var  element=layui.element;
    var laydate=layui.laydate;
    //tab切换
    element.on('tab(doTabBrief)',function(data){
        console.log(data.index)
    });

    laydate.render({
        elem:'#startTime'
        ,value:new Date()
    });

    laydate.render({
        elem:'#endTime'
        ,value:new Date()
    });
    laydate.render({
        elem:'#startTime1'
        ,value:new Date()
    });

    laydate.render({
        elem:'#endTime1'
        ,value:new Date()
    });
    laydate.render({
        elem:'#startTime2'
        ,value:new Date()
    });

    laydate.render({
        elem:'#endTime2'
        ,value:new Date()
    });
    laydate.render({
        elem:'#startTime3'
        ,value:new Date()
    });

    laydate.render({
        elem:'#endTime3'
        ,value:new Date()
    });
    laydate.render({
        elem:'#startTime4'
        ,value:new Date()
    });

    laydate.render({
        elem:'#endTime4'
        ,value:new Date()
    });
    laydate.render({
        elem:'#startTime5'
        ,value:new Date()
    });

    laydate.render({
        elem:'#endTime5'
        ,value:new Date()
    });
    /**
     * 拣货人统计
     */
    var HxErrorPick = {
        tableId: "hxErrorPickTable"
    };

    var HxErrorPick1 = {
        tableId: "storageUser"
    };
    var HxErrorPick2 = {
        tableId: "qualityUser"
    };
    var HxErrorPick3 = {
        tableId: "putUser"
    };
    var HxErrorPick4 = {
        tableId: "offtask"
    };
    var HxErrorPick5 = {
        tableId: "moveStocks"
    };
    /**
     * 初始化表格的列
     */
    HxErrorPick.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: '',  title: '序号',type:"numbers"},
            {field: 'storageUser', sort: true, title: '拣货人账号'},
            {field: 'storageUserName', sort: true, title: '拣货人', totalRowText: '合计：'},
            {field: 'storageNumber', sort: true, title: '单数', totalRow: true},
            {field: 'storageNumbers', sort: true, title: '商品数', totalRow: true},
            {field: 'storageNums', sort: true, title: '数量', totalRow: true},
            {field: 'hourlyWorkload', sort: true, title: '平均每小时', totalRow: true},
            {field: 'uniqueHours', sort: true, title: '时间单位小时', totalRow: true},
            {field: 'piece', sort: true, title: '拆零商品数统计', totalRow: true},
            {field: 'pieceQuantity', sort: true, title: '拆零数量统计', totalRow: true},

        ]];
    };

    /**
     * 点击查询按钮
     */
    HxErrorPick.search = function () {
        var queryData = {};
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['condition'] = $("#condition").val();
        table.reload(HxErrorPick.tableId, {
            where: queryData, page: {curr: 1},
            done:function () {
            }
        });
    };


    /**
     * 导出excel按钮
     */
    HxErrorPick.exportExcel = function () {
        var checkRows = table.checkStatus(HxErrorPick.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    HxErrorPick.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/hxErrorPick/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    HxErrorPick.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/hxErrorPick/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(HxErrorPick.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };
    HxErrorPick.onchuliItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/hxErrorPick/update", function (data) {
                Feng.success("处理成功!");
                table.reload(HxErrorPick.tableId);
            }, function (data) {
                Feng.error("处理失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否处理?", operation);
    };
    // 渲染表格
    var tableResult = table.render({
        elem: '#' + HxErrorPick.tableId,
        url: Feng.ctxPath + '/hxStorageOut/storageUser',
        toolbar: '#toolbarfrom',
        totalRow: true ,//开启合计行
        cellMinWidth: 100,
        cols: HxErrorPick.initColumn()
        ,parseData: function(res){ // 数据转换
            return {
                "code": 0,
                "msg": res.msg, // 假设原数据中的msg字段存储的是提示信息
                "count": res.count,
                "data": $.map(res.data, function(item){ // 假设原数据中的data字段存储的是数据集
                    return {
                        // 假设你想转换的字段是numberField
                        id: item.id,
                        storageUser: item.storageUser,
                        storageUserName: item.storageUserName,
                        storageNumber: Number(item.storageNumber), // 将字符串转换为数字
                        storageNumbers: Number(item.storageNumbers), // 将字符串转换为数字
                        storageNums: Number(item.storageNums), // 将字符串转换为数字
                        hourlyWorkload: Number(item.hourlyWorkload), // 将字符串转换为数字
                        uniqueHours: Number(item.uniqueHours), // 将字符串转换为数字
                        piece: Number(item.piece),
                        pieceQuantity: Number(item.pieceQuantity),

                    };
                })
            };
        },
        page: true,
        limit: 40,
        height: "full-60",

    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        HxErrorPick.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        HxErrorPick.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        HxErrorPick.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + HxErrorPick.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            HxErrorPick.openEditDlg(data);
        } else if (layEvent === 'delete') {
            HxErrorPick.onDeleteItem(data);
        }else if (layEvent === 'chuli'){
            HxErrorPick.onchuliItem(data);
        }
    });



    /**
     * 初始化表格的列
     */
    HxErrorPick1.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: '', title: '序号',type:"numbers"},
            {field: 'qualityUser', sort: true, title: '质检人账号'},
            {field: 'qualityUserName', sort: true, title: '质检人', totalRowText: '合计：'},
            {field: 'storageNumber', sort: true, title: '单数', totalRow: true},
            {field: 'storageNumbers', sort: true, title: '商品数', totalRow: true},
            {field: 'qualityNums', sort: true, title: '数量', totalRow: true},
        ]];
    };

    // 渲染表格
    var tableResult1 = table.render({
        elem: '#' + HxErrorPick1.tableId,
        url: Feng.ctxPath + '/hxStorageOut/qualityUser',

        toolbar: '#toolbarfrom',
        totalRow: true ,//开启合计行
        cellMinWidth: 100,
        cols: HxErrorPick1.initColumn()
        ,parseData: function(res){ // 数据转换
            return {
                "code": 0,
                "msg": res.msg, // 假设原数据中的msg字段存储的是提示信息
                "count": res.count,
                "data": $.map(res.data, function(item){ // 假设原数据中的data字段存储的是数据集
                    return {
                        // 假设你想转换的字段是numberField
                        id: item.id,
                        qualityUser: item.qualityUser,
                        qualityUserName: item.qualityUserName,
                        storageNumber: Number(item.storageNumber),
                        storageNumbers: Number(item.storageNumbers),
                        qualityNums: Number(item.qualityNums),
                    };
                })
            };
        },
        page: true,
        limit: 40,
        height: "full-60",
    });


    /**
     * 点击查询按钮
     */
    HxErrorPick1.search = function () {
        var queryData = {};
        queryData['startTime'] = $("#startTime1").val();
        queryData['endTime'] = $("#endTime1").val();
        queryData['condition'] = $("#condition1").val();
        table.reload(HxErrorPick1.tableId, {
            where: queryData, page: {curr: 1},
            done:function () {
            }
        });
    };
    // 搜索按钮点击事件
    $('#btnSearch2').click(function () {
        HxErrorPick1.search();
        table.reload('storageUser');

    });

    // 导出excel
    $('#btnExp2').click(function () {
        HxErrorPick1.exportExcel();
    });
    /**
     * 导出excel按钮
     */
    HxErrorPick1.exportExcel = function () {
        var checkRows = table.checkStatus(HxErrorPick1.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult1.config.id, checkRows.data, 'xls');
        }
    };
    /**
     * 初始化表格的列
     */
    HxErrorPick2.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: '', title: '序号',type:"numbers"},
            {field: 'qualityUserName', sort: true, title: '质检人', totalRowText: '合计：'},
            {field: 'storageNumbers', sort: true, title: '单数', totalRow: true},
            {field: 'trayNumbers', sort: true, title: '托盘数', totalRow: true},
            {field: 'qualityNums', sort: true, title: '数量', totalRow: true},
        ]];
    };
    // 渲染表格
    var tableResult2 = table.render({
        elem: '#' + HxErrorPick2.tableId,
        url: Feng.ctxPath + '/hxStoragePut/getQualityUser',
        page: true,
        limit: 20,
        height: "full-60",
        totalRow: true ,//开启合计行
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: HxErrorPick2.initColumn()
    });
    /**
     * 点击查询按钮
     */
    HxErrorPick2.search = function () {
        var queryData = {};
        queryData['startTime'] = $("#startTime2").val();
        queryData['endTime'] = $("#endTime2").val();
        queryData['qualityUserName'] = $("#qualityUserName1").val();
        table.reload(HxErrorPick2.tableId, {
            where: queryData, page: {curr: 1},
            done:function () {
            }
        });
    };
    // 搜索按钮点击事件
    $('#btnSearch3').click(function () {
        HxErrorPick2.search();
        table.reload('qualityUser');

    });

    // 导出excel
    $('#btnExp3').click(function () {
        HxErrorPick2.exportExcel();
    });
    /**
     * 导出excel按钮
     */
    HxErrorPick2.exportExcel = function () {
        var checkRows = table.checkStatus(HxErrorPick2.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult2.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 初始化表格的列
     */
    HxErrorPick3.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: '', title: '序号',type:"numbers"},
            {field: 'putUserName', sort: true, title: '上架人', totalRowText: '合计：'},
            {field: 'storageNumbers', sort: true, title: '单数', totalRow: true},
            {field: 'trayNumbers', sort: true, title: '托盘数', totalRow: true},
            {field: 'qualityNums', sort: true, title: '数量', totalRow: true},
        ]];
    };
    // 渲染表格
    var tableResult3 = table.render({
        elem: '#' + HxErrorPick3.tableId,
        url: Feng.ctxPath + '/hxStoragePut/shangjiauser',
        page: true,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        totalRow: true ,//开启合计行
        cellMinWidth: 100,
        cols: HxErrorPick3.initColumn()
    });
    /**
     * 点击查询按钮
     */
    HxErrorPick3.search = function () {
        var queryData = {};
        queryData['startTime'] = $("#startTime3").val();
        queryData['endTime'] = $("#endTime3").val();
        queryData['putUserName'] = $("#putUserName").val();
        table.reload(HxErrorPick3.tableId, {
            where: queryData, page: {curr: 1},
            done:function () {
            }
        });
    };
    // 搜索按钮点击事件
    $('#btnSearch4').click(function () {
        HxErrorPick3.search();
        table.reload('qualityUser');
    });

    // 导出excel
    $('#btnExp4').click(function () {
        HxErrorPick3.exportExcel();
    });
    /**
     * 导出excel按钮
     */
    HxErrorPick3.exportExcel = function () {
        var checkRows = table.checkStatus(HxErrorPick3.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult3.config.id, checkRows.data, 'xls');
        }
    };


    /**
     * 初始化表格的列
     */
    HxErrorPick4.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: '', title: '序号',type:"numbers"},
            {field: 'offUserNames', sort: true, title: '下架人', totalRowText: '合计：'},
            {field: 'trayNumbers', sort: true, title: '托盘数', totalRow: true},
        ]];
    };
    // 渲染表格
    var tableResult4 = table.render({
        elem: '#' + HxErrorPick4.tableId,
        url: Feng.ctxPath + '/hxStorageOffShelf/xiajiauser',
        page: true,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        totalRow: true ,//开启合计行
        cellMinWidth: 100,
        cols: HxErrorPick4.initColumn()
    });
    /**
     * 点击查询按钮
     */
    HxErrorPick4.search = function () {
        var queryData = {};
        queryData['startTime'] = $("#startTime4").val();
        queryData['endTime'] = $("#endTime4").val();
        queryData['offUserName'] = $("#condition4").val();
        table.reload(HxErrorPick4.tableId, {
            where: queryData, page: {curr: 1},
            done:function () {
            }
        });
    };
    // 搜索按钮点击事件
    $('#btnSearch5').click(function () {
        HxErrorPick4.search();
        table.reload('offtask');
    });

    // 导出excel
    $('#btnExp5').click(function () {
        HxErrorPick4.exportExcel();
    });
    /**
     * 导出excel按钮
     */
    HxErrorPick4.exportExcel = function () {
        var checkRows = table.checkStatus(HxErrorPick4.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult4.config.id, checkRows.data, 'xls');
        }
    };


// 点击查询按钮
    HxErrorPick5.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: '', title: '序号',type:"numbers"},
            {field: 'transferUser', sort: true, title: '操作员', totalRowText: '合计：'},
            {field: 'trayNumbers', sort: true, title: '托盘数', totalRow: true},
        ]];
    };
    // 渲染表格
    var tableResult5 = table.render({
        elem: '#' + HxErrorPick5.tableId,
        url: Feng.ctxPath + '/hxStorageMoveRecord/moveUser',
        page: true,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        totalRow: true ,//开启合计行
        cellMinWidth: 100,
        cols: HxErrorPick5.initColumn()
    });
    /**
     * 点击查询按钮
     */
    HxErrorPick5.search = function () {
        var queryData = {};
        queryData['startTime'] = $("#startTime5").val();
        queryData['endTime'] = $("#endTime5").val();
        queryData['transferUser'] = $("#condition5").val();
        table.reload(HxErrorPick5.tableId, {
            where: queryData, page: {curr: 1},
            done:function () {
            }
        });
    };
    // 搜索按钮点击事件
    $('#btnSearch6').click(function () {
        HxErrorPick5.search();
        table.reload('moveStocks');
    });

    // 导出excel
    $('#btnExp6').click(function () {
        HxErrorPick5.exportExcel();
    });
    /**
     * 导出excel按钮
     */
    HxErrorPick5.exportExcel = function () {
        var checkRows = table.checkStatus(HxErrorPick5.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult5.config.id, checkRows.data, 'xls');
        }
    };
    
    $('#btnPrint').click(function () {
        var checkStatus = table.checkStatus('hxErrorPickTable');
        data = checkStatus.data;
        if (data.length > 0) {
            var ids = [];
            for (var i = 0; i < data.length; i++) {
                ids.push(data[i].storageUser);
            }
            layer.open({
                type: 2,
                title: '批量打印用户码',
                area: ['900px', '550px'],
                anim: 5,
                content: Feng.ctxPath + '/basicPosition/tobtnqrCode2?ids=' + ids,
                end: function () {
                }
            });
        } else {
            layer.alert('请至少选中一条数据');
        }
    });




});
